package com.xt.LibaryDeskManager.servlet;

import com.xt.LibaryDeskManager.dto.UserMessage;
import com.xt.LibaryDeskManager.service.StudentService;
import com.xt.LibaryDeskManager.utils.StrUtils;
import com.xt.LibaryDeskManager.utils.Trans;
import com.xt.LibaryDeskManager.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/student")
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;
    /**
     * 按照学院查找学生
     */
    @ResponseBody
    @RequestMapping("/selectByxueyuan")
    public List selectByxueyuan() throws Exception {
        List<UserMessage> list1=studentService.selectAll();
        Map<Integer,List<UserMessage>> map1= Trans.xueyuan(list1);
        List<Integer> list=new ArrayList<>();
        map1.forEach((k,v)->{
            list.add(v.size());
        });
        return list;
    }

    /**
     * 按照年级查找学生
     */
    @ResponseBody
    @RequestMapping("/selectBynianji")
    public List selectBynianji() throws Exception {
        List<UserMessage> list1=studentService.selectAll();
        Map<Integer,List<UserMessage>> map1= Trans.nianji(list1);
        List<Integer> list=new ArrayList<>();
        map1.forEach((k,v)->{
            list.add(v.size());
        });
        return list;
    }

    /**
     * 获取所有学生(分页)
     */
    @ResponseBody
    @RequestMapping("/selectAllStudent")
    public Map selectAllStudent(Integer page,Integer limit,StudentVo studentVo)throws Exception{
         return studentService.selectAll(page,limit,studentVo);
    }

    /**
     * 修改学生状态
     */
    @ResponseBody
    @RequestMapping("/updateValid")
    public Map updateValid(Boolean valid,String studentNo) throws Exception {
        int code=studentService.updateValid(studentNo,valid);
        return StrUtils.creatMessage("code",code);
    }

    /**
     * 更新学生信息
     */
    @ResponseBody
    @RequestMapping("/updateStudent")
    public Map updateStudent(StudentVo studentVo) throws Exception {
        int code=studentService.updateStudent(studentVo);
        return StrUtils.creatMessage("code",code);
    }
    /**
     * 删除学生信息
     */
    @ResponseBody
    @RequestMapping("/deleteStudent")
    public Map deleteStudent(String studentNo)throws Exception{
        int code=studentService.deleteStudent(studentNo);
        return StrUtils.creatMessage("code",code);
    }

    /**
     * 获得座位数量
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/getCount")
    public Map getCount() throws Exception {
        int count=studentService.getCount();
        return StrUtils.creatMessage("count",count);
    }
}
